<?php
class Product_model extends CI_Model {
    public $tabName;

    public function __construct()
    {
        $this->tabName = "sysyaodian.kfsys_products";
    }

    /*
        获取药品分类
        params
            category_id 分类id
    */
    public function getProductCategory($category_id = '')
    {
        $res = array(
            "2" => "口鼻咽喉",
            "3" => "清热解毒",
            "4" => "咳嗽气喘",
            "5" => "眼睛不适",
            "6" => "儿童用药",
            "7" => "皮肤用药",
            "8" => "睡眠调理",
            "9" => "跌打损伤",
            "10" => "外用消毒",
            "11" => "胃肠消化",
            "12" => "妇科用药",
            "13" => "男科用药",
            "14" => "滋补补益",
            "15" => "维矿物质",
            "16" => "泌尿感染",
            "17" => "心脑血管",
            "18" => "风湿骨痛"
        );

        return empty($category_id) ? $res : $res[$category_id];
    }
    /*
        获取药品名称
        params
            product_id[] 药品id数组
    */
    public function getProductNameById($product_id = array())
    {
        $data = array();

        $productList = array();

        $batchCnt = 100;

        foreach ($product_id as $key => $value) {
            $data[] = $value;

            //分批处理数据
            if(count($data) > $batchCnt){
                $sql = "select 
                            ID,Caption 
                        from 
                            " . $this->tabName . " 
                        where 
                            ID in (" . implode(", ", $data) . ")";
                $tmpList = $this->db
                    ->query($sql)
                    ->result_array();
                $productList = array_merge($productList, $tmpList);
                $data = array();
            }
        }
        if(empty($data) == false){
            $sql = "select 
                        ID,Caption 
                    from 
                        " . $this->tabName . " 
                    where 
                        ID in (" . implode(", ", $data) . ")";

            $tmpList = $this->db
                ->query($sql)
                ->result_array();
            $productList = array_merge($productList, $tmpList);
        }
        

        $res = array();

        foreach ($productList as $key => $value) {
            $res[$value['ID']] = $value['Caption'];
        }

        return $res;
    }

    /*
        获取药品信息
        params
            product_id[] 药品id数组
            field 查询字段
    */
    public function getProductInfoById($product_id = array(), $field = "*")
    {
        $data = array();

        $productList = array();

        $batchCnt = 100;

        foreach ($product_id as $key => $value) {
            $data[] = $value;

            //分批处理数据
            if(count($data) > $batchCnt){
                $sql = "select 
                            " . $field . " 
                        from 
                            " . $this->tabName . " 
                        where 
                            ID in (" . implode(", ", $data) . ")";
                $tmpList = $this->db
                    ->query($sql)
                    ->result_array();
                $productList = array_merge($productList, $tmpList);
                $data = array();
            }
        }
        if(empty($data) == false){
            $sql = "select 
                        " . $field . " 
                    from 
                        " . $this->tabName . " 
                    where 
                        ID in (" . implode(", ", $data) . ")";

            $tmpList = $this->db
                ->query($sql)
                ->result_array();
            $productList = array_merge($productList, $tmpList);
        }
        

        $res = array();

        foreach ($productList as $key => $value) {
            $res[$value['ID']] = $value;
        }

        return $res;
    }

    /*
        获取商品库存信息
        params
            store_id 店铺id
            product_id 商品id
    */
    public function getProductStockByStore($store_id = 0)
    {
        $tabName = "sysyaodian.kfsys_productstocks_in_detail";

        $sql = "select 
                    SPID, SPSL 
                from 
                    " . $tabName . " 
                where 
                    KFID = " . $store_id . "";
        $where = array(
            "KFID" => intval($store_id)
        );


        $productInfo = $this->db
            ->query($sql)
            ->result_array();

        $res = array();

        foreach ($productInfo as $key => $value) {
            $product_id = $value['SPID'];
            $stock = $value['SPSL'];
            $res[$product_id] += $stock;
        }

        return $res;

    }
}